<?php

namespace App\Http\Controllers\car;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class OrderController extends Controller
{
    //
    public function index(Request $request){
       //修改购物车中的商品数量
        $sum_price = '';
        $user_id = session('homeuser')->id;
        $checkbox_k = $request->only('checkbox_k');
        $checkbox_k = implode($checkbox_k);
        $checkbox_k = explode(",",$checkbox_k);
        array_pop($checkbox_k);
    	$hobby = $request->only('hobby');
        $num = $request->input('num');
        foreach($checkbox_k as $k=>$v){
            $c = DB::table('car')->where('id',$hobby['hobby'][$k])->pluck('num');
            //向订单表中插入数据
            $product_id = DB::table('car')->where('id',$hobby['hobby'][$k])->pluck('product_id');
                //判断订单表中是否存在该商品,不存在则添加
            $row = DB::table('myorder')->where('product_id',$product_id[0])->where('status',1)->where('user_id',$user_id)->get();
            //dd($row);
            if($row->count()==0){
                $id = DB::table('myorder')->insertGetId(
                ['product_id' => $product_id[0]]
            );
                //dd($id);
            }
                //插入数量
             DB::table('myorder')->where('product_id',$product_id[0])->update(['product_num'=>$num[$v]]);
                //插入用户id
             if(isset($id)){
             $user_id = session('homeuser')->id;
             DB::table('myorder')->where('product_id',$product_id[0])->where('id',$id)->update(['user_id'=>$user_id]);
         }
               //插入总价
             $lower_price = DB::table('product')->where('id',$product_id[0])->pluck('lower_price');
             $total_price = $lower_price[0]*$num[$v];
             $sum_price += $total_price;
              DB::table('myorder')->where('product_id',$product_id[0])->update(['total_price'=>$total_price]);
                //插入商品图片
            $product_pic = DB::table('product')->where('id',$product_id[0])->pluck('picture');
            DB::table('myorder')->where('product_id',$product_id[0])->update(['product_pic'=>$product_pic[0]]);
            //修改购物车数量
            if($c[0]!=$num[$v]){
            DB::table('car')->where('id',$hobby['hobby'][$k])->update(['num'=>$num[$v]]);
            }
        }   
        $list = [];

        foreach($checkbox_k as $k=>$v){
            $product_id = (DB::table('car')->where('id',$hobby['hobby'][$k])->pluck('product_id'))[0];
            $list[] = DB::table('myorder')->leftjoin('product','myorder.product_id', '=', 'product.id')->where('product_id',$product_id)->get();
            //删除购物车中已生成订单的商品
            DB::table('car')->where('product_id',$product_id)->delete();
            
        }
            return view('car.confirm',['list'=>$list,'sum_price'=>$sum_price]);
    }
    public function createaddress(Request $request,$product_id){
        return view('car.newaddress',['product_id'=>$product_id]);
    }
    public function store(Request $request)
    {
        $sum_price = '';
        $product_id = $request->only('product_id');
        $product_id = implode($product_id);
        $product_id = explode(",",$product_id);
        array_pop($product_id);
        $list = [];
        foreach($product_id as $k=>$v){
            $list[] = DB::table('myorder')->leftjoin('product','myorder.product_id', '=', 'product.id')->where('product_id',$v)->get(); 
            $sum_price += $list[$k][0]->lower_price*$list[$k][0]->product_num;
            
        }
    //传过来省份id查省份
        $province = $request->only('province');
        $pro = DB::table('district')->where('id',$province['province'])->get();
        $data = $request->except('_token','product_id','province');
    //省份放入data数组
        $data['province'] = $pro[0]->name;
    //插入地址表
        $m = DB::table('receive_address')->insertGetId($data);
            return view('car.confirm',['list'=>$list,'sum_price'=>$sum_price]);
    }
//ajax获取地址
     public function get()
    {
        return view('car.newaddress');
    }
     public function doget(Request $request)
    {
        $list = DB::table('district')->where('upid',$request->input('upid'))->get();
        echo json_encode($list);
    }
    
    public function dopost(Request $request)
    {
        $list = DB::table('district')->where('upid',$request->input('upid'))->get();
        echo json_encode($list);
    } 
//ajax遍历出用户的地址
    public function aja_user_id(Request $request)
    {
        $user_id = session('homeuser')->id;
        $list = DB::table('receive_address')->where('user_id',$user_id)->get();
        echo json_encode($list);
    }

    	
   
    
}
